package com.shuhua.taskServer.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * Web MVC 配置
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${file.upload.path:./upload}")
    private String uploadPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 创建上传目录
        File uploadDir = new File(uploadPath);
        File avatarsDir = new File(uploadPath + "/avatars");
        File idcardsDir = new File(uploadPath + "/idcards");
        
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        
        if (!avatarsDir.exists()) {
            avatarsDir.mkdirs();
        }
        
        if (!idcardsDir.exists()) {
            idcardsDir.mkdirs();
        }
        
        // 配置静态资源映射
        registry.addResourceHandler("/files/**")
                .addResourceLocations("file:" + uploadDir.getAbsolutePath() + File.separator);
    }
} 